package com.cuotigou.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.cuotigou.config.ConfigUtils;
import com.cuotigou.config.PayConfig;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Created by Administrator on 2019/1/16.
 */
public class FileUtil {
    private static final String SEP1 = "#";
    private static final String SEP2 = "|";
    private static final String SEP3 = "=";

    /**
     * 保存上传的文件
     * @param files
     * @param path
     * @return
     */
    public  static List<String> upload(MultipartFile[] files,String path)  {
        List<String> fileNameList = new ArrayList<>();
        try {
            for (MultipartFile file:files){
                String filename = "image_"+RandomUtil.getRandom(6)+System.currentTimeMillis()+".jpg";
                String formatDate = DateUtil.formatDate(new Date());//获取当前日期
                String foler = path+"/"+formatDate;
                //创建文件夹
                createDirectory(foler);

                File file1 = new File(foler+"/"+filename);
                file.transferTo(file1);
                fileNameList.add(formatDate+"/"+filename);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileNameList;
    }

    /**
     * 创建文件夹
     * @param folderPath
     * @return
     */
    public static boolean createDirectory(String folderPath) {
        boolean isSuccess = false;
        File file = new File(folderPath);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdir();
            isSuccess = true;
        } else {
            isSuccess = true;
        }
        return isSuccess;
    }

    /**
     * 获取txt文件内容
     * @param path
     * @return
     */
    public static String fileRead(String path)  {
        path = Thread.currentThread().getContextClassLoader().getResource(path).getPath();
        File file = new File(path);//定义一个file对象，用来初始化FileReader
        StringBuilder sb = new StringBuilder();//定义一个字符串缓存，将字符串存放缓存中
        try {
            FileReader reader = new FileReader(file);//定义一个fileReader对象，用来初始化BufferedReader
            BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象，将文件内容读取到缓存
            String s = "";
            while ((s =bReader.readLine()) != null) {//逐行读取文件内容，不读取换行符和末尾的空格
                sb.append(s);//将读取的字符串添加换行符后累加存放在缓存中
            }
            bReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static void main(String[] args) throws AlipayApiException {
    }
}
